package com.sskj.one.banker;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.bulong.rudeness.RudenessScreenHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.lib.Constans;
import com.sskj.lib.adapter.BaseAdapter;
import com.sskj.lib.adapter.ViewHolder;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.lib.mvchelper.refresh.MVCSmartHelper;
import com.sskj.one.R;
import com.sskj.one.bean.AssetListBean;
import com.sskj.one.bean.DividendListBean;
import com.sskj.one.bean.GonggaoBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;

/**
 * @author Hey
 * Create at  2019/05/22
 */
public class DividendActivity extends BaseActivity<DividendPresenter> {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.smartRefreshView)
    SmartRefreshLayout smartRefreshView;


    private BaseAdapter<DividendListBean> adapter;

    private int page = 1;

    private String type;

    @Override
    public int getLayoutId() {
        return R.layout.app_activity_dividend;
    }

    @Override
    public DividendPresenter getPresenter() {
        return new DividendPresenter();
    }

    @Override
    public void initView() {
        type = getIntent().getStringExtra("type");
        if (type.equals(Constans.DIVIDEND_HISTORY)) {
            tvTitle.setText("历史分红");
        } else if (type.equals(Constans.DIVIDEND_TODY)) {
            tvTitle.setText("当日分红");
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setRightPadding((int) RudenessScreenHelper.dp2px(this, 15))
                .setLeftPadding((int) RudenessScreenHelper.dp2px(this, 15))
                .setFirstDraw(false)
                .setLastDraw(false)
                .setPaintWidth((int) RudenessScreenHelper.dp2px(this, 1))
                .setPaintColor(ContextCompat.getColor(this, R.color.divide)));
        adapter = new BaseAdapter<DividendListBean>(R.layout.item_dividend, null, recyclerView, true) {
            @Override
            public void bind(ViewHolder holder, DividendListBean item) {
                holder.setText(R.id.tv_msg, item.getMemo())
                        .setText(R.id.tv_date, item.getAddtime())
                        .setText(R.id.tv_money, "$" + item.getPrice());
            }
        };
    }

    @Override
    public void initData() {

        mPresenter.getData(type, page);
        smartRefreshView.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.getData(type, page);
        });
        smartRefreshView.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.getData(type, page);
        });

    }

    public static void start(Context context, String type) {
        Intent intent = new Intent(context, DividendActivity.class);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }

    public void getDataOK(List<DividendListBean> data) {
        if (data == null) {
            return;
        }
        if (page == 1) {
            adapter.setNewData(data);
        } else {
            adapter.addData(data);
        }
    }

    public void stopRefresh() {
        smartRefreshView.finishRefresh();
        smartRefreshView.finishLoadMore();
    }

}
